package br.puc.rio.analiseEspacial;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class ValidaNumeroTextField extends PlainDocument {

	private static final long serialVersionUID = 7621125226071525282L;
	private int maxlength;
	public ValidaNumeroTextField(){}
	
	public ValidaNumeroTextField(int maxlength){
	super();
	this.maxlength = maxlength;
	}

	public void insertString(int offs, String str, AttributeSet a)	{
	try{
	Integer.parseInt(str);
	} catch (NumberFormatException ex)
	{
	
	str = "";
	
	}
	
	try
	
	{
	
	boolean fixedLengh = (!((getLength() + str.length()) > maxlength));
	
	if (maxlength == 0 || fixedLengh)
	
	super.insertString(offs, str, a);
	
	} catch (BadLocationException e)
	
	{
	
	e.printStackTrace();
	}	
	}
	}